package com.android.readcard.write;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.AsyncTask;

import com.android.readcard.inter.ReadIccidListener;
import com.android.readcard.inter.WriteCardListener;

import java.lang.reflect.Method;

import sunrise.bluetooth.SRBluetoothCardReader;

/**
 * 森瑞写卡
 */

public class SenRuiWrite {

    private String blueAddress;
    private Activity mActivity;
    private ReadIccidListener mReadIccidListener;
    private WriteCardListener mWriteCardListener;
    private int mType;

    private SRBluetoothCardReader senmBlueReaderHelper;

    public SenRuiWrite(Activity context, SRBluetoothCardReader senmBlueReaderHelper, int mType,
                       String blueAddress, ReadIccidListener readIccidListener,
                       WriteCardListener writeCardListener) {
        this.mActivity = context;
        this.senmBlueReaderHelper = senmBlueReaderHelper;
        this.mType = mType;
        this.blueAddress = blueAddress;
        this.mReadIccidListener = readIccidListener;
        this.mWriteCardListener = writeCardListener;

    }

    //******************************************读iccid************************************************
    public void connect() {
        boundBlueToothDevice();
        readICCIDTask();//森锐读取白卡ICCID
    }

    /**
     * 绑定蓝牙
     */
    private void boundBlueToothDevice() {
        try {
            BluetoothDevice device = BluetoothAdapter
                    .getDefaultAdapter().getRemoteDevice(blueAddress);
            if (device.getBondState() == BluetoothDevice.BOND_NONE) {
                Method createBondMethod = device.getClass().getMethod(
                        "createBond");
                createBondMethod.invoke(device);
            }
        } catch (Exception e) {
            mReadIccidListener.readIccidErr("绑定蓝牙异常");
            e.printStackTrace();
        }
    }

    /**
     * 读sim卡 森瑞    读取ICCIDD的      order_sn   SendOdNumber
     */
    @SuppressLint("ShowToast")
    protected void readICCIDTask() {

        final byte[] cardNum = new byte[20];
        // 读取ICCID
        new AsyncTask<Object, Object, Integer>() {
            @Override
            protected Integer doInBackground(Object... arg0) {
                if (senmBlueReaderHelper.registerBlueCard(blueAddress)) {
                    return senmBlueReaderHelper.readSimICCID(cardNum);
                } else {
                    mReadIccidListener.readIccidErr("读ICCID连接阅读器失败");
                    return -1;
                }
            }

            @Override
            protected void onPostExecute(Integer result) {
                if (mType == 0) {
                    // 废卡重置
                    if (result == 1) {
                        mReadIccidListener.readIccidSuc(2, new String(cardNum).trim(), senmBlueReaderHelper.queryImsi());
                    } else if (result == 0) {
                        mReadIccidListener.readIccidIsChengKa(1617, new String(cardNum).trim(), "此卡是白卡,无需重置");
                    } else {
                        mReadIccidListener.readIccidErr("读取iccid失败,请重新读卡!" + mType);
                    }
                } else {
                    // 写卡
                    if (result == 1) {
                        mReadIccidListener.readIccidIsChengKa(1617, new String(cardNum).trim(), "此卡是成卡,请换卡重试");
                    } else if (result == 0) {
                        mReadIccidListener.readIccidSuc(2, new String(cardNum).trim(), senmBlueReaderHelper.queryImsi());
                    } else {
                        mReadIccidListener.readIccidErr("读取iccid失败,请重新读卡" + result);
                    }
                }
                super.onPostExecute(result);
            }
        }.execute();
    }
    //******************************写卡*************************************************

    /**
     * 森锐    森锐    森锐写卡方法
     */
    public void writeICCIDTask(final String imei, final String number) {

        new AsyncTask<Object, Object, Boolean>() {
            @Override
            protected Boolean doInBackground(Object... arg0) {
                if (senmBlueReaderHelper.registerBlueCard(blueAddress)) {
                    return senmBlueReaderHelper.writeSimCard(imei, number);
                } else {
                    return false;
                }
            }

            @Override
            protected void onPostExecute(Boolean result) {
                if (result) {
                    mWriteCardListener.writeSuc();
                } else {
                    // 写卡失败
                    mWriteCardListener.writeErr();
                }
                super.onPostExecute(result);
            }
        }.execute();
    }
}
